import request from '../utils/require';

interface User {
  id: number;
  account: string;
  password?: string;
  nickname?: string;
  avatar?: string;
  phone?: string;
}

interface UserInfoResponse {
  success: boolean;
  message?: string;
  data?: User;
}

interface UpdateUserResponse {
  success: boolean;
  message: string;
  data?: User;
}

interface ChangePasswordResponse {
  success: boolean;
  message: string;
}

/**
 * 获取用户信息
 * @param id - 用户ID
 * @returns {Promise} 返回用户信息
 */
export function getUserInfo(id: number): Promise<UserInfoResponse> {
  return request({
    url: '/api/user/info',
    method: 'get',
    params: { id }
  });
}

/**
 * 更新用户信息
 * @param user - 用户信息对象
 * @returns {Promise} 返回更新结果
 */
export function updateUser(user: User): Promise<UpdateUserResponse> {
  return request({
    url: '/api/user/update',
    method: 'put',
    data: user
  });
}

/**
 * 修改密码
 * @param userId - 用户ID
 * @param oldPassword - 原密码
 * @param newPassword - 新密码
 * @returns {Promise} 返回修改结果
 */
export function changePassword(
  userId: number,
  oldPassword: string,
  newPassword: string
): Promise<ChangePasswordResponse> {
  return request({
    url: '/api/user/change-password',
    method: 'post',
    data: {
      userId,
      oldPassword,
      newPassword
    }
  });
}